/*
    18.Write a program in C to Find the Frequency of Characters. 

    Test Data :
    Input the string : This is a test string
    Input the character to find frequency: i

    Expected Output :

    The frequency of 'i' is : 3 
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_LEN 1024

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p++ = ch;
    }
    *p = '\0';
    return str;
}

int countchar(char *str, char ch)
{
    int cnt = 0;
    while(*str){
        if(*str == ch){
            cnt++;
        }
        str++;
    }

    return cnt;
}

int main(void)
{
    char *str = malloc(MAX_LEN);
    char ch;
    printf("Input the string : ");
    getstr(str);
    printf("Input the character to find frequency: ");
    scanf("%c", &ch);
    printf("The frequency of '%c' is : %d\n", ch, countchar(str, ch));

    return 0;
}